package com.example.concurrent.chapter08;

import java.util.concurrent.TimeUnit;

/**
 * @author Liuqz
 * @date 2018/11/22 21:56
 */
public class ThreadPoolTest {

    public static void main(String[] args) throws InterruptedException {

        //定义线程池,初始化线程数为2,核心线程数为4,最大线程数为6,任务队列最多允许1000个任务
        final ThreadPool threadPool = new BasicThreadPool(2, 6, 4, 1000);

        //启动20个任务并且提交到线程池
        for (int i = 0; i < 20; i++) {
            threadPool.execute(() -> {
                try {
                    TimeUnit.SECONDS.sleep(10);
                    System.out.println(Thread.currentThread().getName() + " is running and done.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        for (; ; ) {
            /*System.out.println("getActiveCount : " + threadPool.getActiveCount());
            System.out.println("getQueueSize : " + threadPool.getQueueSize());
            System.out.println("CoreSize : " + threadPool.getCoreSize());
            System.out.println("MaxSize : " + threadPool.getMaxSize());
            System.out.println("===============================");
            TimeUnit.SECONDS.sleep(5);*/

            //应用线程池的销毁功能
            TimeUnit.SECONDS.sleep(12);
            threadPool.shutdown();
        }
    }
}
